package jp.sourceforge.talisman.mds.distance;

/*
 * $Id: NearestNeighborClusterDistanceCalculator.java,v 1.1 2009/09/12 05:06:07 weiwei Exp $
 */

import jp.sourceforge.talisman.mds.Item;
import jp.sourceforge.talisman.mds.cluster.Cluster;

/**
 * 
 * @author Haruaki Tamada
 * @version $Revision: 1.1 $ 
 */
public class NearestNeighborClusterDistanceCalculator extends AbstractClusterDistanceCalculator{
    public NearestNeighborClusterDistanceCalculator(){
    }

    public NearestNeighborClusterDistanceCalculator(ItemDistanceAlgorithm ida){
        super(ida);
    }

    @Override
    public ClusterDistanceAlgorithm getAlgorithm(){
        return ClusterDistanceAlgorithm.NEAREST_NEIGHBOR_METHOD;
    }

    @Override
    public double calculate(Cluster c1, Cluster c2){
        double nearest = Double.MAX_VALUE;

        for(Item item1 : c1){
            for(Item item2 : c2){
                double value = getItemDistanceCalculator().calculate(item1, item2);
                if(nearest > value){
                    nearest = value;
                }
            }
        }
        return nearest;
    }

    @Override
    public double update(Cluster ci, Cluster cj, Cluster ck, double dij, double dik, double djk){
        return dik / 2d + djk / 2d - Math.abs(dik - djk) / 2d;
    }
}
